package com.example.springboot09121.recruitment.controller;
import com.example.springboot09121.recruitment.model.ResultModel;
import com.example.springboot09121.recruitment.model.User;
import com.example.springboot09121.recruitment.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Controller
// 这里不能用 @RestController，不然会无法自动返回 html页面
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @PostMapping("/register")
    public ResultModel<Integer> register(@RequestBody User user) {
        userService.register(user);
        return new ResultModel<>(true, "注册成功");
    }


    @GetMapping("/registerPage")
    public String showRegisterForm() {
        return "register";
    }


    @ResponseBody
    @PostMapping("/login")
    public ResultModel<Optional<User>> login(@RequestBody User user) {
        Optional<User> res_user = userService.login(user);
        return new ResultModel<>(true,"登陆成功",res_user);
    }

    @GetMapping("/loginPage")
    public String showLoginForm() {
        return "login";
    }
}
